package com.shanzmoo.db.sys.service.impl;

import com.shanzmoo.db.sys.entity.RoleEntity;
import com.shanzmoo.db.sys.entity.UserRoleEntity;
import com.shanzmoo.db.sys.mapper.UserRoleMapper;
import com.shanzmoo.db.sys.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户角色关系表 服务实现类
 * </p>
 *
 * @author Gzhao
 * @since 2020-11-10
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleEntity> implements IUserRoleService {

    @Override
    public List<RoleEntity> getUserRoles(Integer userId) {
        return baseMapper.getUserRoles(userId);
    }

    @Override
    public void saveUserRoles(Integer userId, List<Integer> roleIds) {
        baseMapper.deleteByUserId(userId);

        List<UserRoleEntity> list = roleIds.stream().map(roleId -> {
            return new UserRoleEntity(userId, roleId);
        }).collect(Collectors.toList());

        baseMapper.insertBacth(list);
    }
}
